package com.example.demoplus.ui.dashboard;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.bumptech.glide.Glide;
import com.example.demoplus.R;
import com.example.demoplus.api.Song;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<Song> songs;
    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    //    获取显示的布局
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        获取Item布局文件，并且转化为View对象
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_my_app, parent, false);
        return new MyViewHolder(view);
    }
//    绑定数据
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//        获取当前数据
        Song song = this.songs.get(position);
//        找到排名并设置显示内容
        holder.tvIndex.setText(String.valueOf(position+1));//序号
        holder.tvName.setText(song.getName());
        holder.tvSinger.setText(song.getAr().get(0).getName()); //获取到第一个歌手的信息
        Glide.with(context).load(song.getAl().getPicUrl()).into(holder.img);
    }
//    获取数据总量
    @Override
    public int getItemCount() {
        return songs == null ? 0 : songs.size();
    }
//    设置数据
    public void setData(List<Song> songs) {
        this.songs = songs;
        //刷新数据
        notifyDataSetChanged();
    }

    //    创建一个内部类作为泛型
    static class MyViewHolder extends RecyclerView.ViewHolder{
//        排名
        TextView tvIndex;
//        App名字
        TextView tvName;
        TextView tvSinger;
        ImageView img;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        tvIndex = itemView.findViewById(R.id.tv_index);
        tvName = itemView.findViewById(R.id.tv_name);
        tvSinger = itemView.findViewById(R.id.tv_singer);
        img = itemView.findViewById(R.id.img);
    }
}
}
